package com.sanqing.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.sanqing.bean.Reply;
import com.sanqing.dao.ReplyDAO;
import com.sanqing.util.DBConnection;
import com.sanqing.util.Page;

public class ReplyDAOImpl implements ReplyDAO {
/*
 * (non-Javadoc)
 * @see com.sanqing.dao.ReplyDAO#addReply(com.sanqing.bean.Reply)
 * 此方法用于添加回复消息
 */
	@Override
	public void addReply(Reply reply) {
		// TODO Auto-generated method stub
		Connection conn=DBConnection.getConnection();
		String addSQL="insert into tb_reply(replyContent,employeeID,replyTime,messageID) values(?,?,?,?)";
		
		PreparedStatement pstmt=null;
		
		try{
			pstmt=conn.prepareStatement(addSQL);
			pstmt.setString(1, reply.getReplyContent());
			pstmt.setInt(2, reply.getEmployeeID());
			pstmt.setTimestamp(3, new Timestamp(reply.getReplyTime().getTime()));
			pstmt.setInt(4, reply.getMessageID());
			pstmt.executeUpdate();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}finally{
			DBConnection.close(pstmt);
			DBConnection.close(conn);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.sanqing.dao.ReplyDAO#findReplyByMsgID(int, com.sanqing.util.Page)
	 * 次方法用于返回所有回复消息
	 */
	@Override
	public List<Reply> findReplyByMsgID(int messageID, Page page) {
		// TODO Auto-generated method stub
		Connection conn=DBConnection.getConnection();
		String findSQL="select * from tb_reply where messageID=? limit ?,?";
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		
		List<Reply> replays=new ArrayList<Reply>();
		try{
			pstmt=conn.prepareStatement(findSQL);
			pstmt.setInt(1, messageID);
			pstmt.setInt(2, page.getBeginIndex());
			pstmt.setInt(3, page.getEveryPage());
			rs=pstmt.executeQuery();
			while(rs.next())
			{
				Reply reply=new Reply();
				reply.setReplyID(rs.getInt(1));
				reply.setReplyContent(rs.getString(2));
				reply.setEmployeeID(rs.getInt(3));
				reply.setReplyTime(rs.getTimestamp(4));
				reply.setMessageID(rs.getInt(5));
				replays.add(reply);
			}}
			catch(SQLException e)
			{
				e.printStackTrace();
			}finally{
				DBConnection.close(rs);
				DBConnection.close(pstmt);
				DBConnection.close(conn);
			}
		
		return replays;
	}

	/*
	 * (non-Javadoc)
	 * @see com.sanqing.dao.ReplyDAO#findCountByMsgID(int)
	 * 此方法用于统计回复消息个数
	 */
	@Override
	public int findCountByMsgID(int messageID) {
		// TODO Auto-generated method stub
		Connection conn=DBConnection.getConnection();
		String findSQL="select count(*) from tb_reply where messageID=?";
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		int count=0;
		
		try{
			pstmt=conn.prepareStatement(findSQL);
			pstmt.setInt(1, messageID);
			rs=pstmt.executeQuery();
			if(rs.next())
			{
				count=rs.getInt(1);
			}
		}catch(SQLException e)
		{
			e.printStackTrace();
		}finally{
			DBConnection.close(rs);
			DBConnection.close(pstmt);
			DBConnection.close(conn);
		}
		return count;
	}
	
	

}
